package com.itheima.controller.sms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.api.sms.SmsFlashPromotionService;
import com.itheima.vo.domain.AjaxResult;
import com.itheima.vo.domain.TableDataInfo;
import com.itheima.vo.pojo.SmsFlashPromotion;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("flash") //秒杀活动列表
public class SmsFlashPromotionController {

    @DubboReference
    private SmsFlashPromotionService smsFlashPromotionService;

        //根据活动名称分页查询
    @GetMapping("list")
    public AjaxResult page(@RequestParam(value = "keyword", required = false) String keyword,
                           @RequestParam(value = "pageSize") Integer pageSize,
                           @RequestParam(value = "pageNum") Integer pageNum){

        TableDataInfo page = smsFlashPromotionService.page(pageNum, pageSize, keyword);

        return AjaxResult.success(page);
    }

        //添加活动
    @PostMapping("create")
    public AjaxResult create(@RequestBody SmsFlashPromotion smsFlashPromotion){
        //自动获取当前时间
        smsFlashPromotion.setCreateTime(LocalDateTime.now());
        smsFlashPromotionService.save(smsFlashPromotion);

        return AjaxResult.success("添加成功");
    }

    //删除活动
    @PostMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Long id){

        smsFlashPromotionService.removeById(id);

        return AjaxResult.success("删除成功");
    }

    //修改活动
    @PostMapping("/update/{id}")
    public AjaxResult update(@PathVariable Long id,@RequestBody SmsFlashPromotion smsFlashPromotion){

        smsFlashPromotion.setId(id);

        smsFlashPromotionService.update(smsFlashPromotion);

        return AjaxResult.success("修改成功");
    }

    //获取活动详情
    @GetMapping("{id}")
    public AjaxResult findById(@PathVariable Long id){

        SmsFlashPromotion sp = smsFlashPromotionService.getById(id);

        return AjaxResult.success(sp);
    }

   /* //修改活动的上下线状态
    @PostMapping("/update/status/{id}")
    public AjaxResult status(Long status,@PathVariable Long id){

        SmsFlashPromotion smsFlashPromotion = new SmsFlashPromotion();
        smsFlashPromotion.setId(id);
        smsFlashPromotion.setStatus(status);
        smsFlashPromotionService.update(smsFlashPromotion);

        return AjaxResult.success("状态修改成功");
    }*/

    //修改活动的上下线状态(之前错了,这是改正后的)
    @PostMapping("/update/status/{id}")
    public AjaxResult status(Long status,@PathVariable Long id){

        SmsFlashPromotion smsFlashPromotion = new SmsFlashPromotion();
        smsFlashPromotion.setId(id);
        smsFlashPromotion.setStatus(status);
        smsFlashPromotionService.updateStatus(smsFlashPromotion);

        return AjaxResult.success("状态修改成功");
    }
}
